package com.shell.customer.invitation.rx;

import com.shell.customer.base.service.CusLvlService;
import com.shell.customer.base.service.CusRelService;
import com.shell.customer.base.service.CusTagRecService;
import com.shell.customer.invitation.InvtEvent;
import com.shell.customer.po.CusInvt;
import com.shell.customer.po.CusRec;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SetCusRelAndTagSubscriber extends OnErrorNotImplementedSubscriber<InvtEvent> {
	@Autowired
	private CusRelService cusRelService;
	@Autowired
	private CusTagRecService cusTagRecService;
	@Autowired
	private CusLvlService cusLvlService;

	public void onApplicationEvent(InvtEvent event) {
		CusInvt cusInvt = event.getCusInvt();

		CusRec customer = event.getCustomer();
		CusInvt oldCusInvt = event.getOldCusInvt();
		if ((customer != null) && (isExistIvt(oldCusInvt, customer)) && (customer.getCusState().equals("E"))) {
			this.cusRelService.setCusRel(cusInvt, customer);
			this.cusTagRecService.addTagByInvt(cusInvt);
			this.cusTagRecService.syncCusBiz(cusInvt.getInviteeId());
			this.cusLvlService.setCusLvl(cusInvt);
		}
	}

	private boolean isExistIvt(CusInvt oldCusInvt, CusRec customer) {
		return (oldCusInvt == null) || (!customer.getCusName().equals(oldCusInvt.getInviteeName()));
	}
}
